package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// 使用这个类封装针对 user 表的查
public class UserDao {
    // 1. 根据 userId 来查询用户信息. (后续根据博客查询出作者详情)
    public User getUserById(int userId) {
       Connection connection=null;
       PreparedStatement statement=null;
       ResultSet resultSet=null;
       try {
           //建立连接
           connection=DBUtil.getConnection();
           //创建sql语句
           String sql="select* from user where userId=?";
           statement=connection.prepareStatement(sql);
           statement.setInt(1,userId);
           //执行
           resultSet =statement.executeQuery();
           //构造对象
           if(resultSet.next())
           {
               User user=new User();
               user.setUserId(resultSet.getInt("userId"));
               user.setUsername(resultSet.getString("username"));
               user.setPassword(resultSet.getString("password"));
               return user;
           }
       }catch (SQLException e)
       {
           e.printStackTrace();
       }finally {
           //关闭
           DBUtil.close(connection,statement,resultSet);
       }
       return null;
    }

    // 2. 根据 username 来查询用户信息. (实现登陆效果)
    public User getUserByName(String username) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user where username =?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }
    public User deleteUserById(int userId) {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;

        try {
            //建立连接
            connection=DBUtil.getConnection();
            //创建sql语句
            String sql="delete from user where userId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,userId);
            //执行
            statement.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }finally {
            //关闭
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    public void insert(User user)  {
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            //构建链接
            connection = DBUtil.getConnection();
            //构造查询语句
            String sql = "insert into user values(?,?,?)";//获取当前时间，是SQL中提供的
            statement = connection.prepareStatement(sql);
            statement.setInt(1, user.getUserId());
            statement.setString(2, user.getUsername());
            statement.setString(3, user.getPassword());
            //执行
            statement.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }finally {
            //关闭
            DBUtil.close(connection,statement,null);
        }
    }
}
